﻿@namespace BlazorFluentUI
@inherits ButtonParameters
@using Microsoft.AspNetCore.Components.Web

@{
    isSplitButton = (Split && OnClick.HasDelegate && MenuItems != null);
    isCompoundButton = SecondaryText != null;
}

@{
    RenderFragment ButtonContentRederFragment() => __builder =>
    {
        __builder.OpenComponent(0, typeof(ButtonContent));
        __builder.AddAttribute(1, "Href", Href);
        __builder.AddAttribute(2, "ClassName", ClassName);
        __builder.AddAttribute(3, "Style", Style);
        __builder.AddAttribute(4, "Primary", true);
        __builder.AddAttribute(5, "Disabled", Disabled);
        __builder.AddAttribute(6, "AllowDisabledFocus", AllowDisabledFocus);
        __builder.AddAttribute(7, "PrimaryDisabled", PrimaryDisabled);
        __builder.AddAttribute(8, "Checked", Checked);
        __builder.AddAttribute(9, "AriaDescripton", AriaDescripton);
        __builder.AddAttribute(10, "Text", Text);
        __builder.AddAttribute(11, "SecondaryText", SecondaryText);
        __builder.AddAttribute(12, "Toggle", Toggle);
        __builder.AddAttribute(13, "Split", Split);
        __builder.AddAttribute(14, "IconName", IconName);
        __builder.AddAttribute(15, "IconSrc", IconSrc);
        __builder.AddAttribute(16, "HideChevron", HideChevron);
        __builder.AddAttribute(17, "MenuItems", MenuItems);
        __builder.AddAttribute(18, "SubordinateItemTemplate", SubordinateItemTemplate);
        __builder.AddAttribute(19, "CheckedChanged", CheckedChanged);
        __builder.AddAttribute(10, "OnClick", OnClick);
        __builder.AddAttribute(21, "OnKeyDown", OnKeyDown);
        __builder.AddAttribute(22, "Command", Command);
        __builder.AddAttribute(23, "CommandParameter", CommandParameter);
        __builder.AddAttribute(24, "UnknownProperties", UnknownProperties);
        __builder.AddAttribute(25, "ContextMenuShown", contextMenuShown);
        __builder.AddAttribute(26, "ContextMenuShownChanged", Microsoft.AspNetCore.Components.EventCallback.Factory.Create<System.Boolean>(this, Microsoft.AspNetCore.Components.CompilerServices.RuntimeHelpers.CreateInferredEventCallback(this, __value => contextMenuShown = __value, contextMenuShown)));
        if (ContentTemplate != null)
        {
            __builder.AddAttribute(27, "ContentTemplate", ContentTemplate);
        }
        if (MenuItemTemplate != null)
        {
            __builder.AddAttribute(28, "MenuItemTemplate", MenuItemTemplate);
        }
        __builder.AddAttribute(29, "ButtonBase", this);
        __builder.CloseComponent();
    };


    RenderFragment KeytipDataRenderFragment =
    @<KeytipData>

        @if (Href == null)
        {
            <button class=@GetClassNames()
                    disabled=@GetDisabledStatus()
                    @onclick=@ClickHandler
                    @onkeydown=@KeyDownHandler
                    data-is-focusable=@(this.Disabled || PrimaryDisabled || commandDisabled || isSplitButton ? "false" : "true")
                    style=@Style
                    aria-roledescription=@AriaRoleDescription
                    aria-label=@AriaLabel
                    @attributes=@UnknownProperties
                    @ref="RootElementReference">
                @ButtonContentRederFragment()

            </button>
        }
        else
        {
            <a href=@Href
               class=@GetClassNames()
               disabled=@GetDisabledStatus()
               @onclick=@ClickHandler
               @onkeydown=@KeyDownHandler
               data-is-focusable=@(this.Disabled || PrimaryDisabled || commandDisabled || isSplitButton ? "false" : "true")
               style=@Style
               aria-roledescription=@AriaRoleDescription
               aria-label=@AriaLabel
               @attributes=@UnknownProperties
               @ref="RootElementReference">
                @ButtonContentRederFragment()

            </a>
        }
    </KeytipData>;

    @if (isSplitButton)
    {
        <div class="ms-Button-splitContainer" aria-roledescription="@AriaRoleDescription" aria-label="@AriaLabel" data-is-focusable="true" role="button" aria-haspopup="true" aria-expanded="false"
             tabindex=@(!Disabled && !PrimaryDisabled && !commandDisabled ? 0 : -1)>
            <span style="display:flex;">
                @KeytipDataRenderFragment
                @if (Primary)
                {
                    <PrimaryButton IconName="ChevronDown"
                                   AriaLabel="@SplitButtonAriaLabel"
                                   ClassName=@($"ms-Button-menuIcon{(Disabled || commandDisabled ? " is-disabled" : "")} {(isChecked ? " is-checked" : "")}{(contextMenuShown ? " is-expanded" : "")}")
                                   OnClick=@MenuClickHandler
                                   Disabled=@Disabled
                                   tabindex="-1" />
                    <span class=@($"ms-Button-divider ms-Button--primary{(Disabled ? " is-disabled" : "")}")></span>
                }
                else
                {
                    <DefaultButton IconName="ChevronDown"
                                   AriaLabel="@SplitButtonAriaLabel"
                                   ClassName=@($"ms-Button-menuIcon{(Disabled || commandDisabled ? " is-disabled" : "")} {(isChecked ? " is-checked" : "")}{(contextMenuShown ? " is-expanded" : "")}")
                                   OnClick=@MenuClickHandler
                                   Disabled=@Disabled
                                   tabindex="-1" />
                    <span class=@($"ms-Button-divider ms-Button--default{(Disabled ? " is-disabled" : "")}")></span>
                }
            </span>
        </div>
    }
    else
    {
        @KeytipDataRenderFragment
    }
}




@code{
    private string GetClassNames()
    {
        string classNames = "ms-Button";
        if (Primary)
        {
            classNames += " ms-Button--primary";
        }
        else
        {
            classNames += " ms-Button--default";
        }
        if (isSplitButton)
        {
            classNames += $" {this.ClassName} {(Disabled || PrimaryDisabled || commandDisabled ? "is-disabled" : "")} {(isChecked ? "is-checked" : "")}";
        }
        else
        {
            classNames += $" {this.ClassName} {(Disabled || commandDisabled ? " is-disabled" : "")}{(isChecked ? " is-checked" : "")}{(contextMenuShown ? " is-expanded" : "")}";
        }
        return classNames;
    }

    private bool GetDisabledStatus()
    {
        if (isSplitButton)
        {
            return (Disabled || PrimaryDisabled || commandDisabled) && !this.AllowDisabledFocus;
        }
        else
        {
            return (this.Disabled || commandDisabled) && !this.AllowDisabledFocus;
        }
    }


}
